package com.example.travel.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by DELL on 2020/10/28.
 */

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DBName = "contact"; //数据库名称
    private static final int version = 1; //数据库版本
    public static SQLiteDatabase dbInstance=null;//访问数据库的对象

    public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override//在创建数据库后执行onCreate()创建表
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE IF NOT EXISTS user_table (user_id integer primary key autoincrement," +
                " user_name varchar(20), phone txt, image_name text)");
    }

    @Override//在创建数据库后,如果version发生改变，执行onUpgrade修改表
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public static SQLiteDatabase openDatabase(Context context){
        if(dbInstance==null){
            DatabaseHelper db=new DatabaseHelper(context,DBName,null,version);
            dbInstance=db.getWritableDatabase();
        }
        return dbInstance;
    }
}
